function varargout = dhaqtool(varargin)
% DHAQTOOL MATLAB code for dhaqtool.fig
%      DHAQTOOL, by itself, creates a new DHAQTOOL or raises the existing
%      singleton*.
%
%      H = DHAQTOOL returns the handle to a new DHAQTOOL or the handle to
%      the existing singleton*.
%
%      DHAQTOOL(ADAPTORNAME,DEVICEID,FORMAT) constructs a video input
%      object, where FORMAT is a text string that specifies a particular
%      video format supported by the device or the full path of a device
%      configuration file (also known as a camera file).

%      [H VID] = DHAQTOOL returns the handle to the figure and video input
%      object.
%
%      VID = GETAPPDATA(H, 'vid') returns the handle to the video input
%      object using.
%
% See also: GUIDATA, GUIHANDLES


%
%      DHAQTOOL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DHAQTOOL.M with the given input arguments.
%
%      DHAQTOOL('Property','Value',...) creates a new DHAQTOOL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before dhaqtool_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to dhaqtool_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help dhaqtool

% Look for Ini-File
IniFile= which([mfilename '.ini']);
if isempty(IniFile)
    % TODO write real error msg else look for dhaqtool.m and create a ini
    % file.
    error('ini not found');
end

try    
    fig= findobj('tag', mfilename, 'type', 'figure');

    if isempty(fig)
        fig = figure('tag', mfilename, 'visible', 'off');
        setappdata(fig, 'IniFile', IniFile);
        if nargin == 3
            e.Section='VideoInput';
            e.Keys= {'Adaptor', 'DeviceID', 'Format'};
            e.Values= {varargin{1}, varargin{2}, varargin{3}};
            figDhaqtool_IniWrite( fig, e );
        end
        figDhaqtool_CreateFcn(fig, []);  
        set(fig, 'visible', 'on');
    else
        if nargin == 3
            e.Section='VideoInput';
            e.Keys= {'Adaptor', 'DeviceID', 'Format'};
            e.Values= {varargin{1}, varargin{2}, varargin{3}};
            figDhaqtool_IniWrite( fig, e );
            figDhaqtool_VidOpenFcn(fig, e );
        end
    end
catch ex
        if ishandle(fig), delete(fig); end
        error(ex.message);
end

switch nargout 
    case 1, varargout{1}= fig;
    case 2, varargout{1}= fig; varargout{2}= getappdata(fig, 'vid');
end

end